using System;
using System.Collections.Generic;
using System.Text;
using Ent.Web.WebControl;
using Ent.Voice.Nomination;
using System.Web;
using System.Drawing;
using System.Windows.Forms;

namespace Ent.Web.Helper
{
    public class FileHelper
    {
        public static FileType GetFileType(string type)
        {
            switch (type.ToLower())
            { 
                case "image":
                    return FileType.Image;
                case "links":
                    return FileType.Links;
                case "sound":
                    return FileType.Audio;
                case "text":
                    return FileType.Text;
                case "video":
                    return FileType.Video;
            }
            return FileType.Text;
        }
        public static string GetUploadString(Entry entry, DashEntry entryProperty, string serverPath, string basePath, string CommentText)
        {
            if (entry == null)
                return "";
            string startDiv = "<div class='scrolldiv' style='width:" + entryProperty.FileWidth.ToString() + "px;height:" + entryProperty.FileHeight.ToString() + "px;' align='center'>";
            string endDiv = "</div>";
            string commentDiv = "";
            string controlDiv = "";
            string comments = "";
            if (entryProperty.ShowComments)
            {
                foreach (KeyValuePair<string, string> kv in entry.Comments)
                {
                    comments += "&nbsp;&nbsp;&bull;" + kv.Value + " - " + kv.Key;
                }
                commentDiv = (comments.Trim() == "" ? "" : "<div class=\"entry_comments\" style=\"width:" + entryProperty.FileWidth.ToString() + "px;position:absolute;top:" + Convert.ToString(entryProperty.FileHeight - 5) + "px;\"><div class=\"entry_comments_Header\" onclick=\"javascript:ShowComments(" + entry.Id.ToString() + ");\">" + CommentText + "</div><div class=\"entry_comments_content\"><marquee style=\"width:100%;\" scrolldelay=\"200\">" + comments + "</marquee></div></div>");
            }
            if (entryProperty.AllowMaximize)
            {
                controlDiv = "<div class=\"entry_controls\" style=\"width:" + entryProperty.FileWidth.ToString() + "px;position:absolute;top:10px;\"><img onclick=\"javascript:MaximizedView(" + entry.Id.ToString() + ");\" class=\"entry_controls_items\" src=\"" + basePath + "/Images/dj/maximize.jpg\"/></div>";
            }
            string order = "";
            if (entryProperty.OrderBy != -1)
                order = "&o=" + entryProperty.OrderBy.ToString();
            switch (entry.Upload.Type)
            {
                case FileType.Image:
                    string fileLoc = "Uploads/" + entry.Upload.Location;
                    if (entryProperty.ShowThumb)
                    {
                        fileLoc = ImageProcessor.GetThumbnailImage(serverPath, "Uploads/" + entry.Upload.Location,
                            "Uploads/Thumb/" + entry.Upload.Location, entryProperty.FileWidth, entryProperty.FileHeight);
                        return "<a href='" + basePath + "/ShowEntry.aspx?id=" + entry.Id + order + "'><img src=\"" + basePath + "/" + fileLoc + "\" border='0'/></a>";
                    }
                    string style = "";
                    if(entry.Upload.Width > entryProperty.FileWidth)
                    {
                        style = "style=\"width:" + entryProperty.FileWidth.ToString() + "px\"";
                        return startDiv + "<img src=\"" + basePath + "/" + fileLoc + "\" " + style + "/>" + controlDiv + commentDiv + endDiv;
                    }
                    else 
                    {
                        if (entry.Upload.Height > entryProperty.FileHeight)
                        {
                            style = "style=\"height:" + entryProperty.FileHeight.ToString() + "px\"";
                            return startDiv + "<img src=\"" + basePath + "/" + fileLoc + "\" " + style + "/>" + controlDiv + commentDiv + endDiv;
                        }
                        else return "<img src=\"" + basePath + "/" + fileLoc + "\" />" + controlDiv + commentDiv;
                    }
                case FileType.Video:
                case FileType.Audio:
                    if (entryProperty.ShowMedia)
                    {
                        MediaPlayer mp = new MediaPlayer();
                        mp.FileName = "Uploads/" + entry.Upload.Location;
                        mp.Width = entryProperty.FileWidth;
                        mp.Height = entryProperty.FileHeight;
                        mp.ShowControls = entryProperty.ShowControls;
                        mp.AutoStart = entryProperty.AutoStart;
                        commentDiv = (comments.Trim() == "" ? "" : "<div class=\"entry_comments\" style=\"width:" + entryProperty.FileWidth.ToString() + "px;position:absolute;top:" + Convert.ToString(entryProperty.FileHeight - 60) + "px;\"><div class=\"entry_comments_Header\">" + CommentText + "</div><div class=\"entry_comments_content\"><marquee style=\"width:100%;\" scrolldelay=\"200\">" + comments + "</marquee></div></div>");
                        return MediaHelper.GetObjectMedia(mp) + controlDiv + commentDiv;
                    }
                    else
                        return "<a onclick=\"javascript:window.location.href='" + basePath + "/ShowEntry.aspx?id=" + entry.Id + "';\"><img src='" + entryProperty.MediaFile + "'/></a>";
                case FileType.Links:
                    if (entryProperty.ShowThumb)
                    {
                        string desc = "A web page";
                        try { desc = new Uri(entry.Upload.Location).Host; }
                        catch (Exception exc) { }
                        return "<br/><a onclick=\"javascript:window.location.href='" + basePath + "/ShowEntry.aspx?id=" + entry.Id + "';\"><img alt='A web page' src='Images/dj/weblink.gif' title='" + desc + "' style=\"width:20px\"/></a>"; //  + StringHelper.RestrictMaxChar(entry.Upload.Text, entryProperty.EntryMaxChar) + 
                    }
                    else
                    {
                        return (!entryProperty.AllowMaximize ? 
                            "<iframe src='" + entry.Upload.Location + "' style='width:" + entryProperty.FileWidth.ToString() + "px;height:" + entryProperty.FileHeight.ToString() + "px;'></iframe>"
                            : "<img alt=\"" + entry.Upload.Location + "\" src=\"" + basePath + "/GenerateThumb.aspx?id=" + entry.Id.ToString() + "&w=" + entryProperty.FileWidth.ToString() + "&h=" + entryProperty.FileHeight.ToString() + "\" />") + controlDiv + commentDiv;
                        //GenerateScreenshot(entry.Upload.Location, entryProperty.FileWidth, entryProperty.FileHeight)
                    }
                case FileType.Text:
                    string text = StringHelper.RestrictMaxChar(HttpUtility.HtmlDecode(entry.Upload.Text), entryProperty.EntryMaxChar);
                    StringHelper.ConvertToHtmlText(ref text);
                    if (entryProperty.ShowThumb)
                    {

                        return "<font class='text10 white'><div align='left'>" + text + "</div></font>";
                    }
                    else
                    {
                        return startDiv + "<font class='text11 white'><div align='left'>" + text + "</div></font>" + controlDiv + commentDiv + endDiv;
                    }
                default:
                    break;
            }
            return "";
        }
    }
}
